<template>
  <el-form ref="formobj" :model="wyform" :rules="rules" label-width="100px">
    <el-form-item label="药品名称" prop="drugName">
      <el-input v-model="wyform.drugName" maxlength="64" placeholder="请输入药品名称" disabled></el-input>
    </el-form-item>
    <el-form-item label="药品单价" prop="drugPrice">
      <el-input v-model="wyform.drugPrice" maxlength="64" placeholder="请输入药品单价"></el-input>
    </el-form-item>
    <el-form-item label="药品余量" prop="quantity">
      <el-input v-model="wyform.quantity" maxlength="64" placeholder="请输入药品余量"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save" >修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref,defineEmits } from 'vue'
import { post,get } from '@/util/axios'
import {url} from './api.js'
const wyform = reactive({
  drugName: '',
  drugPrice: '',
  quantity: ''
})
const emits=defineEmits(['update:show','getTableData'])
const props=defineProps(['id'])

const check=(rule,value,callback,field)=>{
  get(url.check,{field,value,id:props.id},content=>{
    if (content){
      callback()
    }else{
      callback(new Error())
    }
  })

}
const rules = reactive({
  drugPrice: [
    { required: true, message: '请输入药品单价', trigger: 'blur' },
  ],
  quantity: [
    { required: true, message: '请输入药品余量', trigger: 'blur' }
  ]
})
const formobj = ref()
if (props.id){
  getById()
}

function getById(){
  get('/drugInfo/getById', {id:props.id}, content => {
        for(const key in wyform){
          if (Object.prototype.hasOwnProperty.hasOwnProperty.call(content,key)){
            wyform[key]=content[key]
          }
        }
    wyform.id=props.id
    console.log(content)
  }, formobj)
}

function save () {
    post('/drugInfo/updateById', wyform, content => {
      emits('update:show',false),
      emits('getTableData')
    }, formobj)
}
</script>

<style scoped lang="scss">
</style>
